package com.bw.liuyushan.zhuangao4_weektest2.Curve;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.HardwareRenderer;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;

import com.bw.liuyushan.zhuangao4_weektest2.R;

import java.util.Random;

/**
 * ZhuanGao4_WeekTest2
 * file name is : MyView
 * created by Ender on 2021/8/26 13:49.
 * author : 刘雨杉
 * Describe(作用):自定义View-贝赛尔曲线动画 4个点 S形运动
 * */
public class MyView extends RelativeLayout {
    private Drawable[] drawables = new Drawable[5];
    private int width;
    private  int height;
    public MyView(Context context) {
        super(context);
        init();
    }

    private void init() {//添加图片数组 不同图片看效果
        drawables[0] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher_round);
        drawables[1] = ContextCompat.getDrawable(getContext(),R.drawable.xin);
        drawables[2] = ContextCompat.getDrawable(getContext(),R.drawable.xink);
        drawables[3] = ContextCompat.getDrawable(getContext(),R.drawable.xin2);
        drawables[4] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
    }
    public void addImageView(){
        ImageView imageView = new ImageView(getContext());
        imageView.setImageDrawable(drawables[new Random().nextInt(drawables.length)]);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(60,60));
        addView(imageView);

        getAnimator(imageView).start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getMeasuredWidth();
        height=getMeasuredHeight();
    }

    private ValueAnimator getAnimator(ImageView imageView) {//起始点 终点
        MyEvaluator myEvaluator = new MyEvaluator(getPointF(),getPointF());
        PointF StartPointF = new PointF((float) (Math.random()*200), height);
        PointF EndPointF = new PointF(new Random().nextInt(width), new Random().nextInt(200));

        ValueAnimator valueAnimator = ValueAnimator.ofObject(myEvaluator,StartPointF,EndPointF);
        valueAnimator.setDuration(2000);
        valueAnimator.setTarget(imageView);
        valueAnimator.setRepeatCount(5);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
                imageView.setAlpha(1-animation.getAnimatedFraction());

            }
        });
        return valueAnimator;
    }
    public PointF getPointF(){
        PointF pointF = new PointF();
        pointF.x = (float) (Math.random()*width);
        pointF.y = (float) (Math.random()*(height/4));
        return pointF;
    }


    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    class  MyEvaluator implements TypeEvaluator<PointF>{
        private PointF pointFa;
        private PointF pointFb;

        public MyEvaluator(PointF pointFa, PointF pointFb) {
            this.pointFa = pointFa;
            this.pointFb = pointFb;
        }

        /**
         * 参考 三阶贝塞尔曲线的数学公式 链接↓
         * https://blog.csdn.net/cfan927/article/details/104649623/
         *
         */
        @Override//贝塞尔曲线的中间两个点 靠三阶贝赛尔曲线公式
        public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
            float time=1.0f- fraction;
            PointF point =new PointF();
            point.x=time*time*time*(startValue.x)
                    +3*time*time*fraction*(pointFa.x)
                    +3*time*fraction*fraction*(pointFb.x)
                    +time*time*time*(endValue.x);
            point.y=time*time*time*(startValue.y)
                    +3*time*time*fraction*(pointFa.y)
                    +3*time*fraction*fraction*(pointFb.y)
                    +time*time*time*(endValue.y);
            return point;
        }
    }
}
